 #include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include<QLabel>
#include<QPushButton>
#include<string>
#include<iostream>
#include<QDebug>
#include<QKeyEvent>
#include <QPainter>
#include <stdlib.h>
#include <time.h>
#include<QMediaPlayer>
#include<QDialog>
#include<QMessageBox>
#include<QTimerEvent>
//全局
#define ZOOM 1

#define POINT 100

int X,Y;
int nowTime = 0,steps=0;
int timerId;
MainScene::MainScene(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainScene)
{

    ui->setupUi(this);
    //退出
    connect(ui->actiontuic,&QAction::triggered,[=](){
        this->close();
    });
    //新游戏
    connect(ui->actiond,&QAction::triggered,[=](){
        this->CreatNewGame();
    });
    //开发者信息
    connect(ui->action1_2,&QAction::triggered,[=]{
        QMessageBox::information(this,"开发者信息","开发者:2111792 张钧瑞");
    });
    //游戏说明
    connect(ui->action1,&QAction::triggered,[=](){
        QMessageBox::information(this,"游戏说明","按“↑”、“↓”、“←”、“→”对空白方块进行操作");
    });
    this->setFixedSize(480*ZOOM,640*ZOOM);
    this->setWindowIcon(QPixmap(":/game/MNLS.png"));
    this->setWindowTitle("拼图");
    //打印输出框
 init();

    //显示拼图
    this->update();
}
//初始化操作
void MainScene::init(){
    X=NUM-1;
    Y=NUM-1;
    //设置界面
    for(int i=0;i<NUM;i++){
        for(int j=0;j<NUM;j++){
            Game[i][j]=i*NUM+j;
        }
    }
    //设置步数和时间的字体
    QPalette pe;
    pe.setColor(QPalette::WindowText, Qt::white);
    ui->label->move(90,530);
    ui->label_2->move(340,530);
    ui->label->setPalette(pe);
    ui->label_2->setPalette(pe);


    CreatNewGame();
//    timerId=startTimer(1000);
}

void MainScene::CreatNewGame(){
    //打乱的拼图
    steps=0;
    ui->label_2->setText(QString::number(steps));
    srand((unsigned)time(NULL));
    //增加游戏打乱随机度
    for(int i=0;i<RANDTIME;i++){
           int r=rand() % NUM;
        if(r==0&&Y>0){
            GoUp();
        }else if(r==1&&Y<NUM-1){
            GoDown();
        }else if(r==2&&X>0){
            GoLeft();
        }else if(r==3&&X<NUM-1){
            GoRight();
        }

    }
    //开始计时
    killTimer(timerId);
    timerId=startTimer(1000);
    ui->label->setText("0");
    //开始声音
    StartSound();
    nowTime = 0;

    //游戏开始
}